<?php

namespace app\models;

use yii\db\ActiveRecord;

/**
 * 卡属性
 * Class CardAttr
 * @package app\models
 *
 * @property integer $id PK
 * @property integer $uid 用户编号
 * @property string $name 属性名称
 * @property integer $show_in_list 是否在列表中显示
 * @property integer $type 类型
 * @property integer $sort 排序
 * @property string $options 备选值
 * @property string $desc 描述
 * @property integer $status 状态
 */
class CardAttr extends ActiveRecord
{
    const TYPE_PASSWORD = 1;
    const TYPE_TEXT = 2;
    const TYPE_MULTI_TEXT = 3;
    const TYPE_RICH_TEXT = 4;
    const TYPE_NUMBER = 5;
    const TYPE_EMAIL = 6;
    const TYPE_URL = 7;
    const TYPE_RADIO = 8;
    const TYPE_CHECKBOX = 9;
    const TYPE_DATETIME = 10;
    const TYPE_DATE = 11;
    const TYPE_TIME = 12;
    const TYPE_FILE = 13;
    const TYPE_IMAGE = 14;
    const TYPE_COLOR = 15;

    const STATUS_OK = 1;
    const STATUS_DEL = 0;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'show_in_list', 'type', 'sort'], 'required'],
            ['name', 'string', 'max' => 32],
            [['options', 'desc'], 'safe'],
            ['show_in_list', 'integer'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'name' => '属性名称',
            'show_in_list' => '是否在列表中显示',
            'type' => '类型',
            'sort' => '排序',
            'options' => '备选值',
            'desc' => '描述',
        ];
    }

    /**
     * 根据类型展示不同格式的内容
     * @param string $value 属性值
     * @return string
     */
    public function getHtml($value)
    {
        if (empty($value)) {
            return '';
        }
        switch ($this->type) {
            case CardAttr::TYPE_PASSWORD:
            case CardAttr::TYPE_TEXT:
            case CardAttr::TYPE_MULTI_TEXT:
            case CardAttr::TYPE_RICH_TEXT:
            case CardAttr::TYPE_NUMBER:
            case CardAttr::TYPE_EMAIL:
            case CardAttr::TYPE_URL:
            case CardAttr::TYPE_RADIO:
            case CardAttr::TYPE_CHECKBOX:
            case CardAttr::TYPE_DATETIME:
            case CardAttr::TYPE_TIME:
            case CardAttr::TYPE_FILE:
            case CardAttr::TYPE_IMAGE:
            case CardAttr::TYPE_COLOR:
            default:
                return $value;
        }
    }
}
